package info.magnolia.blossom.sample.cv.components;

import info.magnolia.blossom.sample.cv.ListOfCVs;
import info.magnolia.blossom.sample.cv.model.CurriculumVitae;
import info.magnolia.blossom.sample.cv.service.CVService;
import info.magnolia.module.blossom.annotation.Template;
import info.magnolia.module.blossom.annotation.TemplateDescription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * User: peterbalazs
 * Date: 2/26/13
 * Time: 11:06 AM
 * To change this template use File | Settings | File Templates.
 */
@Template(title = "Skills", id = "blossomSample:components/skills")
@TemplateDescription("Skills and aptitudes component")
@Controller
public class SkillsComponent {

    private static final String ERROR_MSG = "errorMsg";
    private static final String ERROR_PAGE_PATH = "components/error.jsp";

    @Autowired
    private CVService cvService;

    @RequestMapping("/cvtemplate/skills")
    public String render(final ModelMap model, final HttpServletRequest request) {
        final String id = request.getParameter(ListOfCVs.PARAM_CV_ID);
        final CurriculumVitae cv = cvService.getCV(id);

        if (cv == null) {
            model.put(ERROR_MSG, "There is nothing to view");
            return ERROR_PAGE_PATH;
        }

        model.put("cv", cv);
        return "components/skills.jsp";
    }
}
